package com.example.study.common.enums;

import com.example.study.SpringUtils;
import com.example.study.service.MessageService;
import lombok.Data;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public enum MiniProgramEnum {

    EXHIBITION("XINONE","XINONEACCESSTOKENSTRATEGY",
            "com.kering.cus.rsvp.subscription.message.center.service.impl.ExhibitionMessageServiceImpl"),
    STORE("CRM","CRMACCESSTOKENSTRATEGY",
            "com.kering.cus.rsvp.subscription.message.center.service.impl.StoreMessageServiceImpl"),
    ;
    @Getter
    private String miniapp;
    @Getter
    private String strategy;
    @Getter
    private String clazz;

    MiniProgramEnum(String miniapp, String strategy, String clazz) {
        this.miniapp = miniapp;
        this.strategy = strategy;
        this.clazz = clazz;
    }


    public static String getStrategyByMiniapp(String miniapp){
        MiniProgramEnum[] values = MiniProgramEnum.values();
        for (MiniProgramEnum t : values) {
            if (miniapp.equals(t.getMiniapp())) {
                return t.getStrategy();
            }
        }
        return null;
    }

    public static MessageService getMessageService(String miniapp){
        MiniProgramEnum[] values = MiniProgramEnum.values();
        try{
            for (MiniProgramEnum t : values) {
                if (miniapp.equals(t.getMiniapp())) {
                    Class<?> clazz = Class.forName(t.getClazz());
                    Object object = SpringUtils.getBean(clazz);
                    return (MessageService)object;
                }
            }
        }catch (ClassNotFoundException e){
            log.error("MiniProgramEnum.getMessageService error:{}",e);
        }
        return null;
    }
}

